package com._101aiot.common.resultutil;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * 通用返回对象
 */
@Data
@ApiModel(value = "统一响应消息报文")
public class CommonResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "是否成功", required = true)
    private boolean success;

    @ApiModelProperty(value = "状态码", required = true)
    private String code;

    @ApiModelProperty(value = "消息", required = true)
    private String message;

    // 此注解，null值不参与序列化
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "业务数据")
    private T data;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(value = "时间", required = true)
    private Date time;

    protected CommonResult(boolean success, String code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
        this.time = new Date();
    }

    /**
     * 成功无数据返回
     */
    public static <T> CommonResult<T> success() {
        return new CommonResult<T>(ResultCode.SUCCESS.getSuccess(), ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);
    }

    /**
     * 请求成功，未查询到数据
     * @param <T> T
     * @return 返回结果
     */
    public static <T> CommonResult<T> successNoData() {
        return new CommonResult<T>(ResultCode.SUCCESS_NO_DATA.getSuccess(), ResultCode.SUCCESS_NO_DATA.getCode(), ResultCode.SUCCESS_NO_DATA.getMessage(), null);
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(ResultCode.SUCCESS.getSuccess(), ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data    获取的数据
     * @param message 提示信息
     */
    public static <T> CommonResult<T> success(T data, String message) {
        return new CommonResult<T>(ResultCode.SUCCESS.getSuccess(), ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     */
    public static <T> CommonResult<T> failed(IResultCode errorCode) {
        return new CommonResult<T>(errorCode.getSuccess(), errorCode.getCode(), errorCode.getMessage(), null);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     * @param message   错误信息
     */
    public static <T> CommonResult<T> failed(IResultCode errorCode, String message) {
        return new CommonResult<T>(errorCode.getSuccess(), errorCode.getCode(), message, null);
    }

    /**
     * 错误码，错误信息
     * @param code 错误码
     * @param message 消息
     * @param <T> T
     * @return T
     */
    public static <T> CommonResult<T> failed(String code, String message) {
        return new CommonResult<T>(false, code, message, null);
    }

    /**
     * 异常拦截 包装后的异常现象
     * @param code 错误码
     * @param message 消息
     * @param data 数据
     * @param <T> 类型
     * @return T
     */
    public static <T> CommonResult<T> failed(String code, String message, T data) {
        return new CommonResult<T>(false, code, message, data);
    }

    /**
     * 失败返回错误消息和错误提示
     * @param message 错误提示
     * @param data 数据
     * @param <T> T
     * @return T
     */
    public static <T> CommonResult<T> failed(String message, T data) {
        return new CommonResult<T>(false, ResultCode.BUSINESS_FAILURE.getCode(), message, data);
    }

    /**
     * 异常信息构造
     * @param success 是否成功
     * @param code 状态码
     * @param message 错误信息
     * @param data 错误数据
     * @param <T> T
     * @return 返回结构
     */
    public static <T> CommonResult<T> failed(boolean success, String code, String message, T data) {
        return new CommonResult<T>(success, code, message, data);
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<T>(ResultCode.FAILED.getSuccess(), ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 业务错误返回
     * @param message 消息
     * @param <T> T
     * @return T
     */
    public static <T> CommonResult<T> businessFailed(String message) {
        return new CommonResult<T>(ResultCode.BUSINESS_FAILURE.getSuccess(), ResultCode.BUSINESS_FAILURE.getCode(), message, null);
    }

    /**
     * 业务错误返回带数据
     * @param message 消息
     * @param data 数据
     * @param <T> T
     * @return T
     */
    public static <T> CommonResult<T> businessFailed(String message, T data) {
        return new CommonResult<T>(ResultCode.BUSINESS_FAILURE.getSuccess(), ResultCode.BUSINESS_FAILURE.getCode(), message, data);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResult<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> CommonResult<T> validateFailed(String message) {
        return new CommonResult<T>(ResultCode.SUCCESS.getSuccess(), ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    /**
     * 参数校验不通过
     * @param data 数据
     * @param <T> 泛型
     * @return T
     */
    public static <T> CommonResult<T> validateFailed(T data) {
        return new CommonResult<T>(ResultCode.PARAMETER_CHECK_FAILED.getSuccess(), ResultCode.PARAMETER_CHECK_FAILED.getCode(), ResultCode.PARAMETER_CHECK_FAILED.getMessage(), data);
    }

    /**
     * 参数校验不通过
     * @param data 数据
     * @param message 消息
     * @param <T> 泛型
     * @return T
     */
    public static <T> CommonResult<T> validateFailed(T data, String message) {
        return new CommonResult<T>(ResultCode.PARAMETER_CHECK_FAILED.getSuccess(), ResultCode.PARAMETER_CHECK_FAILED.getCode(), message, data);
    }

    /**
     * 未登录返回结果
     */
    public static <T> CommonResult<T> unauthorized(T data) {
        return new CommonResult<T>(ResultCode.UNAUTHORIZED.getSuccess(), ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
    }

    /**
     * 未授权返回结果
     */
    public static <T> CommonResult<T> forbidden(T data) {
        return new CommonResult<T>(ResultCode.FORBIDDEN.getSuccess(), ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
    }

}
